package top.boycharse.blog.controller.manager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import top.boycharse.blog.bean.Response.BaseResponse;
import top.boycharse.blog.bean.Response.LabelResponse;
import top.boycharse.blog.bean.Response.ResponseCode;
import top.boycharse.blog.bean.request.LabelInfo;
import top.boycharse.blog.common.Constants;
import top.boycharse.blog.dao.bean.Label;
import top.boycharse.blog.dao.bean.User;
import top.boycharse.blog.dao.service.LabelService;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/manager/label")
public class ManagerLabelController {

    private static final String LABEL_IS_EXIT = "标签已存在！";

    private static final String LABEL_NOT_EXIT = "标签不存在！";

    @Autowired
    private LabelService labelService;

    /**
     * 获取所有标签
     * @return BaseResponse
     */
    @GetMapping("/getAllLabel")
    public BaseResponse<?> getAllLabel() {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        List<Label> labels = labelService.findLabelByUsername(username);
        List<LabelResponse> labelResponses = new ArrayList<>();
        for (Label temp : labels) {
            labelResponses.add(new LabelResponse(temp));
        }
        return new BaseResponse<>(labelResponses);
    }

    /**
     * 创建一个label
     * @param info
     * @return
     */
    @PostMapping("/createLabel")
    public BaseResponse<?> createLabel(@RequestBody LabelInfo info) {
        if (StringUtils.isEmpty(info.getName())) {
            return BaseResponse.buildParamException();
        }
        Label label = labelService.findByLabelName(info.getName());
        if (label != null) {
            return BaseResponse.buildFailMsg(ResponseCode.CODE_FAIL,LABEL_IS_EXIT);
        }

        label = new Label(info);
        // 获得创建用户的信息
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        User user = new User(name);
        label.setUser(user);
        label = labelService.saveLabel(label);
        LabelResponse response = new LabelResponse(label.getId(),label.getLabelName(),label.getUser().getUserName());
        return new BaseResponse<>(response);
    }

    @PostMapping("/deleteLabel")
    public BaseResponse<?> deleteLabel(@RequestParam("id")Integer id) {
        Label label = labelService.findByLabelId(id);
        if (label == null) {
            return BaseResponse.buildFailMsg(ResponseCode.CODE_FAIL,LABEL_NOT_EXIT);
        }

        labelService.deleteLabel(label);

        return BaseResponse.buildSuccessMsg(Constants.DELETE_SUCCESS);
    }

    @PostMapping("/updateLabel")
    public BaseResponse<?> updateLabel(@RequestBody Label info) {
        String labelName = info.getLabelName();
        if (StringUtils.isEmpty(labelName)) {
            return BaseResponse.buildParamException();
        }
        Label label = labelService.findByLabelName(labelName);
        if (label != null) {
            return BaseResponse.buildFailMsg(ResponseCode.CODE_FAIL,LABEL_IS_EXIT);
        }
        label = labelService.findByLabelId(info.getId());
        if (label == null) {
            return BaseResponse.buildFailMsg(ResponseCode.CODE_FAIL,LABEL_NOT_EXIT);
        }
        labelService.updateLabelName(label,labelName);
        LabelResponse response = new LabelResponse(label);
        return new BaseResponse<>(response);
    }
}
